<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="结算日期" prop="settlementTime">
        <el-date-picker clearable
          v-model="queryParams.settlementTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结算日期">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="订单总金额" prop="totalPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalPrice"-->
<!--          placeholder="请输入订单总金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="订单数量" prop="orderNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.orderNum"-->
<!--          placeholder="请输入订单数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="平台服务费总额" prop="serviceCharge">-->
<!--        <el-input-->
<!--          v-model="queryParams.serviceCharge"-->
<!--          placeholder="请输入平台服务费总额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付服务费总额" prop="payServiceFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.payServiceFee"-->
<!--          placeholder="请输入支付服务费总额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="可提现金额" prop="withdrawPrice">
        <el-input
          v-model="queryParams.withdrawPrice"
          placeholder="请输入可提现金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否确认" prop="isAck">
        <el-input
          v-model="queryParams.isAck"
          placeholder="请输入是否确认"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否申请提现" prop="isWithdraw">
        <el-input
          v-model="queryParams.isWithdraw"
          placeholder="请输入是否申请提现"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="提现申请时间" prop="withdrawTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.withdrawTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择提现申请时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item label="是否打款" prop="isRemit">
        <el-input
          v-model="queryParams.isRemit"
          placeholder="请输入是否打款"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="打款时间" prop="remitTime">
        <el-date-picker clearable
          v-model="queryParams.remitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择打款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="打款人" prop="remitPerName">
        <el-input
          v-model="queryParams.remitPerName"
          placeholder="请输入打款人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="打款账户" prop="remitAccount">-->
<!--        <el-input-->
<!--          v-model="queryParams.remitAccount"-->
<!--          placeholder="请输入打款账户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="收款账户" prop="collectionAccount">-->
<!--        <el-input-->
<!--          v-model="queryParams.collectionAccount"-->
<!--          placeholder="请输入收款账户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="流水号" prop="serialNumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.serialNumber"-->
<!--          placeholder="请输入流水号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['settlement:settlement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['settlement:settlement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['settlement:settlement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['settlement:settlement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="settlementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="结算编号" align="center" prop="settlementId" />
      <el-table-column label="结算日期" align="center" prop="settlementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settlementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付渠道" align="center" prop="payType" >
        <template slot-scope="scope">
          <b v-if="scope.row.payType==1" style="color: #1ab394">微信</b>
          <b v-if="scope.row.payType==2" style="color: #C03639">支付宝</b>
        </template>
      </el-table-column>
      <el-table-column label="订单总金额" align="center" prop="totalPrice" />
      <el-table-column label="订单数量" align="center" prop="orderNum" />
      <el-table-column label="平台服务费总额" align="center" prop="serviceCharge" />
      <el-table-column label="支付服务费总额" align="center" prop="payServiceFee" />
      <el-table-column label="可提现金额" align="center" prop="withdrawPrice" />
      <el-table-column label="是否确认" align="center" prop="isAck" >
        <template slot-scope="scope">
          <b v-if="scope.row.isAck==0" style="color: red">未确认</b>
          <b v-if="scope.row.isAck==1" style="color: #00afff">已确认</b>
        </template>
      </el-table-column>
      <el-table-column label="是否申请提现" align="center" prop="isWithdraw" >
        <template slot-scope="scope">
          <b v-if="scope.row.isWithdraw==0" style="color: red">未申请</b>
          <b v-if="scope.row.isWithdraw==1" style="color: #00afff">已申请</b>
        </template>
      </el-table-column>
      <el-table-column label="提现申请时间" align="center" prop="withdrawTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.withdrawTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否打款" align="center" prop="isRemit">
        <template slot-scope="scope">
          <b v-if="scope.row.isRemit==0" style="color: #a95812">未打款</b>
          <b v-if="scope.row.isRemit==1" style="color: #1890ff">已打款</b>
        </template>
      </el-table-column>
      <el-table-column label="打款时间" align="center" prop="remitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="打款人" align="center" prop="remitPerName" />
      <el-table-column label="打款账户" align="center" prop="remitAccount" />
      <el-table-column label="收款账户" align="center" prop="collectionAccount" />
      <el-table-column label="流水号" align="center" prop="serialNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="isOk(scope.row.settlementId)"
            v-if="scope.row.isAck==0"
          >金额确认</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['settlement:settlement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['settlement:settlement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改结算对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="结算日期" prop="settlementTime">
          <el-date-picker clearable
            v-model="form.settlementTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结算日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单总金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入订单总金额" />
        </el-form-item>
        <el-form-item label="订单数量" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="平台服务费总额" prop="serviceCharge">
          <el-input v-model="form.serviceCharge" placeholder="请输入平台服务费总额" />
        </el-form-item>
        <el-form-item label="支付服务费总额" prop="payServiceFee">
          <el-input v-model="form.payServiceFee" placeholder="请输入支付服务费总额" />
        </el-form-item>
        <el-form-item label="可提现金额" prop="withdrawPrice">
          <el-input v-model="form.withdrawPrice" placeholder="请输入可提现金额" />
        </el-form-item>
        <el-form-item label="是否确认" prop="isAck">
          <el-input v-model="form.isAck" placeholder="请输入是否确认" />
        </el-form-item>
        <el-form-item label="是否申请提现" prop="isWithdraw">
          <el-input v-model="form.isWithdraw" placeholder="请输入是否申请提现" />
        </el-form-item>
        <el-form-item label="提现申请时间" prop="withdrawTime">
          <el-date-picker clearable
            v-model="form.withdrawTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提现申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否打款" prop="isRemit">
          <el-input v-model="form.isRemit" placeholder="请输入是否打款" />
        </el-form-item>
        <el-form-item label="打款时间" prop="remitTime">
          <el-date-picker clearable
            v-model="form.remitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择打款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="打款人" prop="remitPerName">
          <el-input v-model="form.remitPerName" placeholder="请输入打款人" />
        </el-form-item>
        <el-form-item label="打款账户" prop="remitAccount">
          <el-input v-model="form.remitAccount" placeholder="请输入打款账户" />
        </el-form-item>
        <el-form-item label="收款账户" prop="collectionAccount">
          <el-input v-model="form.collectionAccount" placeholder="请输入收款账户" />
        </el-form-item>
        <el-form-item label="流水号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入流水号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSettlement, getSettlement, delSettlement, addSettlement, updateSettlement, update} from "@/api/settlement/settlement";

export default {
  name: "Settlement",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 结算表格数据
      settlementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settlementTime: null,
        payType: null,
        totalPrice: null,
        orderNum: null,
        serviceCharge: null,
        payServiceFee: null,
        withdrawPrice: null,
        isAck: null,
        isWithdraw: null,
        withdrawTime: null,
        isRemit: null,
        remitTime: null,
        remitPerName: null,
        remitAccount: null,
        collectionAccount: null,
        serialNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    //金额确认按钮
    isOk(settlementId){
      update(settlementId).then(resp=>{
        if (resp.code==200){
          alert("确认成功")
          this.getList()
        }else {
          alert("确认失败")
        }
      })
    },
    /** 查询结算列表 */
    getList() {
      this.loading = true;
      listSettlement(this.queryParams).then(response => {
        this.settlementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        settlementId: null,
        settlementTime: null,
        payType: null,
        totalPrice: null,
        orderNum: null,
        serviceCharge: null,
        payServiceFee: null,
        withdrawPrice: null,
        isAck: null,
        isWithdraw: null,
        withdrawTime: null,
        isRemit: null,
        remitTime: null,
        remitPerName: null,
        remitAccount: null,
        collectionAccount: null,
        serialNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.settlementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加结算";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const settlementId = row.settlementId || this.ids
      getSettlement(settlementId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改结算";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.settlementId != null) {
            updateSettlement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettlement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const settlementIds = row.settlementId || this.ids;
      this.$modal.confirm('是否确认删除结算编号为"' + settlementIds + '"的数据项？').then(function() {
        return delSettlement(settlementIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('settlement/settlement/export', {
        ...this.queryParams
      }, `settlement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
